<script setup>
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
// ------------------------------用户搜索
const formInline = reactive({
    userId: '',
    username: '',
    nickname: '',
    regst: 'all',
    reg: 'all',
    hot: 'all',
    dis: 'all',
    sup: 'all',
    dic: 'all',
    regTime: ''
})

const onSubmit = () => {
    console.log('submit!')
}
// ------------------------------用户数据
let users = reactive([
    {
        sort: 111, id: '秀我中华爱我中华（9）', hbtypes: '手气', fftypes: '立即', 
        amount: '100/100', num: '10/10',tips: '恭喜发财，大吉大利', 
        regTime: '2024-07-14 12:27:56'
    },
    {
        sort: 110, id: '秀我中华爱我中华（9）', hbtypes: '手气', fftypes: '立即', 
        amount: '90/100', num: '8/10',tips: '恭喜发财，大吉大利', 
        regTime: '2024-07-25 12:27:56'
    },
    {
        sort: 109, id: '高桂玲（38046）', hbtypes: '手气', fftypes: '立即', 
        amount: '10/100', num: '10/10',tips: '恭喜发财，大吉大利', 
        regTime: '2024-05-29 12:27:56'
    },
    {
        sort: 108, id: '白高平（2492）', hbtypes: '手气', fftypes: '立即', 
        amount: '20/100', num: '1/10',tips: '恭喜发财，大吉大利', 
        regTime: '2024-05-22 10:25:36'
    },
    {
        sort: 107, id: '石景文（37805）', hbtypes: '手气', fftypes: '立即', 
        amount: '100/100', num: '10/10',tips: '恭喜发财，大吉大利', 
        regTime: '2024-05-10 6:57:36'
    },
    {
        sort: 106, id: '赵延莉（38625）', hbtypes: '手气', fftypes: '立即', 
        amount: '20/20', num: '2/2',tips: '恭喜发财，大吉大利', 
        regTime: '2024-04-01 21:30:34'
    },
    {
        sort: 105, id: '赵延莉（38625）', hbtypes: '手气', fftypes: '立即', 
        amount: '100/100', num: '10/10',tips: '恭喜发财，大吉大利', 
        regTime: '2024-04-01 21:20:44'
    },
    {
        sort: 104, id: '秀我中华爱我中华（9）', hbtypes: '手气', fftypes: '立即', 
        amount: '1000/1000', num: '20/20',tips: '恭喜发财，大吉大利', 
        regTime: '2024-04-01 21:10:34'
    },
    {
        sort: 103, id: '秀我中华爱我中华（9）', hbtypes: '手气', fftypes: '立即', 
        amount: '1000/1000', num: '20/20',tips: '恭喜发财，大吉大利', 
        regTime: '2024-04-01 21:00:48'
    },
    
])

// ------------------------ 分页

const currentPage = ref(1)
const pageSize = ref(100)
const size = ref('default')
const background = ref(false)
const disabled = ref(false)

const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
}

// ---------------------- 删除用户操作
// 弹出二次确认对话框：参数user就是要删除的用户：
function showConfirm(user) {
    ElMessageBox.confirm(
    '确定要删除 ' + user.id +' 的红包记录吗?',
    '警告信息',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      // 调用删除函数：删除用户
      deleteUser(user)
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '管理员取消了删除操作',
      })
    })
    
}

// 删除用户的函数：参数user就是要删除的用户
function deleteUser(user) {
    // 查询用户在数组中的索引
    let index = users.findIndex(item => item.id === user.id)
    // 删除用户数据
    users.splice(index, 1)
    // 提示删除成功
    ElMessage({
        type: 'success',
        message: '管理员成功删除了 ' + user.id + ' 的红包记录',
      })
}

// // ------------------ 新增用户操作 
// import DialogUserCreate from '../components/DialogUserCreate.vue'
// // 声明变量控制弹窗显示/隐藏
// let userDialogCreate = ref(false)
// // 准备函数：显示对话框
// function showCreateDialog() {
//     userDialogCreate.value = true
// }
// // 准备函数：隐藏对话框
// function hideCreateDialog() {
//     userDialogCreate.value = false
// }
// // 新增用户数据：参数user就是子组件告诉父组件要新增的用户
// function createUser(user) {
//     // 计算新用户的id
//     let id = users[0].id + 1
//     // 复制一份用户数据
//     let newUser = {...user}
//     // 给新数据添加id属性
//     newUser.id = id
//     // 新增用户
//     users.unshift(newUser)
// }


</script>
<template>
    <div class="p-header">
        <h2>红包管理</h2>
        <el-button type="primary" @click="showCreateDialog">新增红包记录</el-button>
    </div>
    <!-- 新增用户弹窗 -->
    <DialogUserCreate :dialogFormVisible="userDialogCreate"
        @createUserEvent="createUser"
        @closeCreateEvent="hideCreateDialog"></DialogUserCreate>

    <el-divider>红包信息搜索</el-divider>
    <el-card>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="会员：">
                <el-input v-model="formInline.userId" placeholder="请输入会员ID、靓号" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
            </el-form-item>
        </el-form>
    </el-card>
    <el-divider>红包信息展示</el-divider>
    <el-table :data="users" style="width: 100%">
        <el-table-column label="编号" prop="sort" width="80"/>
        <el-table-column label="发起者（ID）" prop="id" width="300"/>
        <el-table-column label="红包类型" prop="hbtypes" width="120"/>
        <el-table-column label="发放类型" prop="fftypes" width="120"/>
        <el-table-column label="已抢/总金额" prop="amount" width="120"/>
        <el-table-column label="已抢/总个数" prop="num" width="120"/>
        <el-table-column label="描述" prop="tips" width="180"/>
        <el-table-column label="生效时间" prop="regTime" width="220"/>
        <el-table-column label="添加时间" prop="regTime" width="220"/>
        <!-- {
        sort: 111, id: '秀我中华爱我中华（9）', hbtypes: '手气', fftypes: '立即', 
        amount: '100/100', num: '10/10',tips: '恭喜发财，大吉大利', 
        regTime: '2024-08-25 12:27:56',addTime: '2024-08-25 12:28:32'
    } -->
        <el-table-column align="left" fixed="right" width="220" label="操作">
            <template #default="scope">
                <el-button-group>
                    <el-button type="success" size="small">领取记录</el-button>
                    <el-button type="danger" size="small" @click="showConfirm(scope.row)">删除记录</el-button>
                </el-button-group>
            </template>
        </el-table-column>
    </el-table>

    <el-pagination
      background
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[100, 200, 300, 400]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="1000"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
</template>
<style scoped>
.demo-form-inline .el-input {
    --el-input-width: 220px;
}

.demo-form-inline .el-select {
    --el-select-width: 100px;
    font-size: 14px;
}

.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}

.el-pagination{
    margin-top:20px;
}

.p-header{
    display:flex;
    justify-content: space-between;
}
</style>